package com.aqie.easy.math;

import java.util.Arrays;

/**
 * 1103 每个小朋友分糖+1，直到最后一个小朋友 2 * n颗糖
 * todo
 */
public class DistributeCandies {
    /**
     * 错误 只考虑两轮
     * @param candies
     * @param num_people
     * @return
     */
    public static int[] distributeCandies2(int candies, int num_people) {
        int[] ans = new int[num_people];
        // 1. 先判断是否够分两轮的
        if (2* Math.pow(num_people,2) + num_people <= candies){
           for (int i = 1; i <= num_people; i++){
               ans[i - 1] = num_people + 2 * i;
           }
           return ans;
        }

        if (Math.pow(num_people, 2) + num_people >= 2*candies) {
            int sum = 0;
            for (int i = 1; i <= num_people; i++) {
                sum += i;
                if (sum < candies) {
                    ans[i - 1] = i;

                } else {
                    ans[i - 1] = candies + i - sum;
                    break;
                }
            }
            return ans;
        }else {
            for (int i = 1; i <= num_people; i++){
                ans[i - 1] = i;
            }
            double res =  candies - (Math.pow(num_people, 2) + num_people) / 2;
            int sum = 0;
            for (int i = 1; i <= num_people; i++){
                sum+= (i+num_people);
                if (sum < res){
                    ans[i - 1] += (i+num_people);
                }else{
                    ans[i - 1] += res + (i + num_people) - sum;
                    break;
                }
            }
            return ans;
        }
    }


    public static int[] distributeCandies(int candies, int num_people) {

        int[] candy = new int[num_people];

        int yilun = (1 + num_people) * num_people / 2;

        int j = 0;

        int tmp = candies;

        while (tmp - yilun - j * num_people * num_people > 0) {
            tmp = tmp - yilun - j * num_people * num_people;
            j++;
        }


        for (int i = 0; i < num_people; i++) {
            candy[i] = (i + 1 + i + 1 + (j - 1) * num_people) * j / 2;
            if (tmp >= i + 1 + j * num_people) {
                candy[i] += i + 1 + j * num_people;
                tmp = tmp - i - 1 - j * num_people;
            } else {
                candy[i] += tmp;
                tmp = 0;
            }
        }

        return candy;

    }
    public static void main(String[] args) {
        int candies = 15;
        int num_people = 5;
        /**
         * [1, 2, 3, 4, 5]
         * [2, 2, 3, 4, 5]
         * [7, 9, 11, 13, 15]
         * [15, 18, 15, 12]
         */
        // 1. 糖果不够分一轮的
        System.out.println(Arrays.toString(distributeCandies(candies, num_people)));
        // 2. 糖果只够分一轮的
        candies = 16;
        System.out.println(Arrays.toString(distributeCandies(candies, num_people)));
        // 3. 糖果够分两轮的
        candies = 55;
        System.out.println(Arrays.toString(distributeCandies(candies, num_people)));

        candies = 60;
        num_people = 4;  // (n^2 + n)/2 = 10;
        System.out.println(Arrays.toString(distributeCandies(candies, num_people)));
    }
}
